package com.solarStruggle.domain

/**
 * A vehicle is the template used to define a unit.  It is a composition of shards.
 */
class Vehicle {

	def totalCost: Int = allShards.foldLeft (0) { (a, b) => a + b.cost }

	def totalResearchCost: Int = allShards.foldLeft (0) { (a, b) => a + b.researchCost }

	def totalMass: Int = allShards.foldLeft (0) { (a, b) => a + b.mass }

	def totalEnergy: Int = allShards.foldLeft (0) { (a, b) => a + b.energy }

	def maxEngines: Int = chassis.foldLeft(0) { (a, b) => a + b.maxEngines }

	def maxWeapons: Int = chassis.foldLeft(0) { (a, b) => a + b.maxWeapons }

	def maxPods: Int = chassis.foldLeft(0) { (a, b) => a + b.maxPods }

	def availableEngines: Int = maxEngines - engines.length

	def availableWeapons: Int = maxWeapons - weapons.length

	def availablePods: Int = maxPods - pods.length

	/**
	 * The frame of the vehicle
	 */
	var chassis: Seq[Chassis] = Nil

	var material: Seq[Material] = Nil

	var shield: Seq[Shield] = Nil

	var engines: Seq[Engine] = Nil

	var weapons: Seq[Weapon] = Nil

	var pods: Seq[Pod] = Nil

	var name: String = ""

	def allShards: Seq[Shard] = 
		chassis ++
		material ++
		shield ++
		engines ++
		weapons ++
		pods

	/**
	 * Returns a list of error messages for any aspects of the vehicle which are invalid, or Nil
	 * if there are no errors.
	 */
	def validate(): List[VehicleError] = 
		(if(chassis.isEmpty) List(new VehicleError("Vechicle must have a frame")) else Nil) :::
		Nil
}

/**
 * A problem with a vehicle configuration
 */
case class VehicleError(text: String)
